package com.java.test.agent.module.info;

import com.java.test.agent.module.apply.entity.AgentApplyEntity;
import com.java.test.agent.module.info.dto.AgentCensusDTO;
import com.java.test.agent.module.info.dto.AgentInfoDTO;
import com.java.test.agent.module.info.dto.AgentInfoQueryDTO;
import com.java.test.agent.module.info.dto.AgentInfoUpdateDTO;
import com.java.test.agent.module.info.entity.AgentInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author lijie1
 * @date 2020/6/30 0030
 */
@Component
public class AgentInfoDao {

    private JdbcTemplate jdbcTemplate;
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<AgentApplyEntity> selectByList() {
        StringBuffer sql = new StringBuffer("select * from agent_info t where  1 = 1 ");
        List<AgentApplyEntity> info = new ArrayList<AgentApplyEntity>();;
        List<Object> param = new ArrayList();
        try {
            info= jdbcTemplate.query(sql.toString(), param.toArray(), new AgentApplyEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<AgentInfoEntity> getSelectByList(AgentInfoQueryDTO queryDTO) {
        StringBuffer sql = new StringBuffer("select * from agent_info t where  1 = 1 ");
        List<AgentInfoEntity> info = new ArrayList<AgentInfoEntity>();;
        List<Object> param = new ArrayList();
        try {
            if(queryDTO.getAccount()!=null && !"".equals(queryDTO.getAccount())){
                sql.append(" and t.account = ? ");
                param.add(queryDTO.getAccount());
            }
            if(queryDTO.getStatus()!=null && !"".equals(queryDTO.getStatus())){
                sql.append(" and t.status = ? ");
                param.add(queryDTO.getStatus());
            }
            sql.append("limit ? , ?");
            int startSize = (queryDTO.getPageNum()*queryDTO.getPageSize())-queryDTO.getPageSize();
            int endSize = (queryDTO.getPageNum()*queryDTO.getPageSize());
            param.add(startSize);
            param.add(endSize);
            info= jdbcTemplate.query(sql.toString(), param.toArray(), new AgentInfoEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 查询当前id是否存在
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public List<AgentInfoEntity> getByIds(String ids) {

        String sql = "select * from agent_info t where t.id in("+ids+")";

        List<AgentInfoEntity> list = new ArrayList<>();
        try {
            list = jdbcTemplate.query(sql,new AgentInfoEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return list;
    }

    /**
     * 修改
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public void updateById(int status,String ids) {
        String sql = "update agent_info " +
                "set  status = ? " +
                "where id in("+ids+")";

        jdbcTemplate.update(sql,status);
    }

    /**
     * 获取代理信息
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public AgentInfoEntity getEntity(AgentInfoDTO dto) {
        String sql = "select * from agent_info where id in("+dto.getIds()+")";
        AgentInfoEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new AgentInfoEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 修改费率
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public void updateScale(AgentInfoUpdateDTO dto) {
        String sql = "update agent_info " +
                "set  feescale = ? ,brokeragescale = ?" +
                "where id = ?";

        jdbcTemplate.update(sql,dto.getFeeScale(),dto.getBrokerageScale(),dto.getId());
    }

    /**
     * 获取代理商信息
     * @author lijie1
     * @date 2020/7/4 0004
     */
    public List<Map<String, Object>> getAgentCensusByNew(AgentCensusDTO dto) {
        List<Object> param = new ArrayList();
        StringBuffer sql = new StringBuffer("select count(1) num from agent_info t where 1 = 1 ");
        if(dto.getEndTime()!=null && !"".equals(dto.getEndTime())){
            sql.append(" and DATE_FORMAT(t.createTime,'%Y-%m-%d') <= STR_TO_DATE( ? ,'%Y-%m-%d') ");
            param.add(dto.getEndTime());
        }
        if(dto.getStartTime()!=null && !"".equals(dto.getStartTime())){
            sql.append(" and DATE_FORMAT(t.createTime,'%Y-%m-%d') >= STR_TO_DATE( ? ,'%Y-%m-%d') ");
            param.add(dto.getStartTime());
        }
        return jdbcTemplate.queryForList(sql.toString(), param.toArray());
    }
}
